#ifndef SWITCH_SCRIPT_OBJECT_H_
#define SWITCH_SCRIPT_OBJECT_H_

#include "script_object_base.h"

// Switch script object class for all switch features, such as
// validate quick launches, delete a quick launch, generate a 
// quick launch, etc.
class SwitchScriptObject : public ScriptObjectBase {
private:
  SwitchScriptObject(void) : update_state_(false) {}
  virtual ~SwitchScriptObject(void) {}

public:
#ifdef _UNICODE
  struct QuickLaunchItem {
    std::string id;
    std::wstring file_path;
    std::wstring file_name;
    std::wstring icon_file;
    int icon_index;
    std::wstring params;
    std::string theme_name;
    std::string theme_url;
  };
#else
  struct QuickLaunchItem {
    int id;
    std::string file_path;
    std::string file_name;
    std::string icon_file;
    int icon_index;
    std::string params;
    std::string theme_name;
    std::string theme_url;
  };
#endif

  enum ErrorCode {
    kInvalidParameters  = 1000,
    kInvalidUserDataDir,
    kInvalidThemeHelperDir,
    kCopyThemeHelperFailed,
    kWriteThemeJsFailed,
    kInvalidIconFile,
    kExistsSameLinkFile,
    kSaveLinkFailed,
    kNotChromeQuickLaunch,
    kDeleteOriginalLinkFailed,
    kCopyManifestFileFailed,
  };

  enum StateType {
    kStateInvalid,
    kKeyPrefixStart,
    kKeyPrefixEnd,
    kKeyStart,
    kKeyEnd,
    kQuoteValueStart,
    kQuoteValueEnd,
    kValueStart,
    kValueEnd,
  };

public:
  static NPObject* Allocate(NPP npp, NPClass *aClass); 
  void InitHandler();

  void Deallocate();
  void Invalidate() {}
  bool Construct(const NPVariant *args,uint32_t argCount,
                 NPVariant *result) { return true; }

  // Validate quich launches.
  bool ValidateQuickLaunches(const NPVariant *args, uint32_t argCount,
                             NPVariant *result);

  // Delete a quick launch.
  bool DeleteQuickLaunch(const NPVariant *args, uint32_t argCount,
                         NPVariant *result);

  // Generate a quick launch.
  bool GenerateQuickLaunch(const NPVariant *args, uint32_t argCount,
                           NPVariant *result);

  // Update a quick launch.
  bool UpdateQuickLaunch(const NPVariant *args, uint32_t argCount,
                         NPVariant *result);

  // Prompt user to select a path, and return it to front-end.
  bool GetLocalPath(const NPVariant *args, uint32_t argCount,
                    NPVariant *result);

  // Select a local launch and return the parameters to front-end.
  bool ReadLocalLaunch(const NPVariant *args, uint32_t argCount,
                       NPVariant *result);

  // Startup a quick launch.
  bool Startup(const NPVariant *args, uint32_t argCount, NPVariant *result);

  // Validating inputs is a directory.
  bool ValidPath(const NPVariant *args, uint32_t argCount, NPVariant *result);

  // Get user desktop folder.
  bool GetDesktopFolder(const NPVariant *args, uint32_t argCount, 
                        NPVariant *result);

  // Get quick launch folder.
  bool GetQuickLaunchFolder(const NPVariant *args, uint32_t argCount, 
                            NPVariant *result);

  // Get start program folder.
  bool GetStartProgramFolder(const NPVariant *args, uint32_t argCount, 
                             NPVariant *result);

  // Get our specified user-data-dir
  bool GetUserDataFolder(const NPVariant *args, uint32_t argCount, 
                         NPVariant *result);

  // Get chrome's default data path.
  bool GetDefaultDataPath(const NPVariant *args, uint32_t argCount,
                          NPVariant *result);

  // Select an local icon from exe, dll or ico file, and return the icon data
  // to front-end.
  bool GetLocalIcon(const NPVariant *args, uint32_t argCount,
                    NPVariant *result);

  // Get default chrome icon base64 data.
  bool GetDefaultChromeIcon(const NPVariant *args, uint32_t argCount,
                            NPVariant *result);

  // Get icon base64 data from a quick launch.
  bool GetIconDataFromQuickLaunch(const NPVariant *args, uint32_t argCount,
                                  NPVariant *result);

  // Current system is win7.
  bool IsWin7System(const NPVariant *args, uint32_t argCount,
                    NPVariant *result);

private:

  // Prepare Json key and value.
  void PrepareJsonValue(std::string& value);

  // Prepare icon provided by extension.
  bool PrepareIcon(const TCHAR* iconname, TCHAR* dest_iconfile);

  // Get object value by name
  bool GetObjectProperty(NPP npp, NPObject* object, const char* name, 
                         int* value);
  bool GetObjectProperty(NPP npp, NPObject* object, 
                         const char* name, std::string* value);
  bool GetObjectProperty(NPP npp, NPObject* object, 
                         const char* name, std::wstring* value);

  bool GetThemeName(TCHAR* dir, std::string* themename);

  bool update_state_;

};

#endif